package edu.sctu.practice.class01.wulixia.month11_wlx.guava;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;

/**
 * Created by wulixia on 2015/11/26.
 */
public class Client {
    public static void main(String[] args) {
        // http://www.bxwx.org/b/70/70093/index.html

        String url = "http://www.bxwx.org/b/70/70093/index.html";

        try {
            Document document = Jsoup.connect(url).timeout(5000).get();

            Elements elements = document.select("dd > a");

            for(Element element:elements) {
                String title = element.text();
                String detailUrl = element.attr("href");

                System.out.println(element.text());
                System.out.println(element.attr("href"));
               //将内容导入文档
                Files.append(element.text() + "-" + element.attr("href"), new File("E:\\123.txt"),
                        Charsets.UTF_8);

                //实现内容换行
                Files.append("\r\n", new File("E:\\123.txt"), Charsets.UTF_8);
//                Files.write("hello".getBytes(), new File("E:\\小说\\" + title + ".txt"));

                getContent(title,detailUrl);
            }
            System.out.println(document);

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    private static void getContent(String title, String detailUrl) {
        try {
            Document document = Jsoup.connect( "http://www.bxwx.org/b/70/70093/" + detailUrl)
                    .timeout(5000).get();

            Element element = document.select("div#content").get(0);
            String content = element.text();

            Files.createParentDirs(new File("E:\\小说\\demo.txt"));
            Files.write(content.getBytes(), new File("E:\\小说\\" + title + ".txt"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
